<?php

/**
 * SemesterExamine Model
 *
 * Utilizes the Data Mapper pattern to persist data. Represents a single
 * semester_examine entry
 *
 * @version $Id: SemesterExamine.php 123 2010-05-30 11:41:51Z yingkuang.chen $
 * @license New BSD License
 * @uses Semester_Model_SemesterExamineMapper
 * @package semester
 * @subpackage Model
 */
class Semester_Model_SemesterExamine extends Semester_Model_Doctrine_BaseSemesterExamine
{
    /**
     *  定期評量
     * @var integer
     */
    const REGULAR_SCORE = 1;
    /**
     * 平時成績
     * @var integer
     */
    const USUALLY_SCORE  = 2;
    /**
     * 全學期成績
     * @var integer
     */
    const WHOLE_SEMESTER_SCORE  = 3;

    /**
     * 每階段採相同比率
     */
    const SAME_RATE = 1;

    /**
     * 每階段採不同比率
     * @var integer
     */
    const DIFFERENT_RATE = 2;


    public function getDefaultJudge()
    {
        $locale = Bootstrap::$config['resources']['locale']['default'];
        $path = dirname(__FILE__)."/../languages/$locale/data.ini";
        $ini = new Zend_Config_Ini($path);
        $arr = $ini->get('array_data')->toArray();;
        return $arr['judge'] ;
    }

    /**
     * //設定該學科考次,定期平時比率
     * @return unknown_type
     */
    public function getExaminSet($year,$semester,$grade)
    {
        $q = $this->getTable()->createQuery('a')
        ->Where('year=?', $year)
        ->andWhere('semester=?', $semester)
        ->andWhere('grade=?',$grade);
        $examine = $q->fetchOne();

        $ratioArr = array();
        $ratioArr['all'] = 0;
        // 每次評量均採相同比例
        if ($examine->mode == Semester_Model_SemesterExamine::SAME_RATE) {
            $tempArr = explode("-",$examine->ratio);
            for($i=1; $i<= $examine->period; $i++) {
                $ratioArr[$i]['1'] = $tempArr[0];
                $ratioArr[$i]['2'] = $tempArr[1];
                $ratioArr['all'] += $tempArr[0] + $tempArr[1];
            }
        }
        //每次評量採不同比例
        else {
            $tempArr = explode(",",$examine->ratio);

            foreach($tempArr as $id=>$val) {
                $tempArr2 = explode('-',$val);
                $i = $id+1;
                $ratioArr[$i]['1'] = $tempArr2[0];
                $ratioArr[$i]['2'] = $tempArr2[1];
                $ratioArr['all'] += $tempArr2[0] + $tempArr2[1];
            }
        }

        $examine->ratio =  $ratioArr;

        return $examine;
    }


    /**
     * 解析等第
     * @param float $score 分數
     * @param string $judge 判斷值
     */
    public static function resolveJudgement($score , $judge)
    {
        $res = array();
        $arr = explode("\n", $judge);
        foreach ($arr as $row) {
            $rule_a=array();
            $str = explode("_",$row);
            $dubleStr = (double)$str[2];

            if($str[1]==">="){
                if($score >= $dubleStr)return $str[0];
            }elseif($str[1]==">"){
                if($score > $dubleStr)return $str[0];
            }elseif($str[1]=="="){
                if($score == $dubleStr)return $str[0];
            }elseif($str[1]=="<"){
                if($score < $dubleStr)return $str[0];
            }elseif($str[1]=="<="){
                if($score <= $dubleStr)return $str[0];
            }
        }
        return '';
    }
}
